<template>
    <div class="login-wrap">
        <Header></Header>
        <div class="login-box">
            <div class="container">
                <div class="logo">
                    <img src="../../assets/logo.png" alt="">
                    <p>以科技之光照亮学习盲区，用智能引擎驱动成长加速度</p>
                </div>
                <div class="login-form">
                    <div class="login-tab">
                        <div class="account" :class="isAccount ? 'active ' : ''" @click="isAccount = true">
                            账号登录
                        </div>
                        <div class="shouji" :class="!isAccount ? 'active ' : ''" @click=" isAccount = false">
                            手机号登录
                        </div>
                    </div>
                    <div class="form-item">
                        <a-form v-if="isAccount" :form="form" class="formclass">
                            <a-form-item>
                                <a-input v-decorator="['account',{ rules: [{ required: true, message: '请输入账号' }] },]" placeholder="请输入账号"></a-input>
                            </a-form-item>
                            <a-form-item>
                                <a-input v-decorator="['password',{ rules: [{ required: true, message: '请输入密码' }] },]" :type="typePwd" placeholder="请输入密码">
                                    <a-icon slot="suffix" @click="showPwd" type="eye" />
                                </a-input>
                            </a-form-item>
                            <a-form-item>
                                <a-checkbox v-decorator="['remember', { valuePropName: 'checked', initialValue: false,},]">
                                    同意<router-link to="/contactUs?id=1">《用户服务协议》</router-link><router-link to="/contactUs?id=2">《隐私协议》</router-link>
                                </a-checkbox>
                            </a-form-item>
                        </a-form>
                        <a-form v-else :form="form" class="formclass">
                            <a-form-item>
                                <a-input v-decorator="['mobile', { rules: [{ required: true, message: '请输入手机号' }]}]" style="width: 100%" placeholder="请输入手机号">
                                    <a-select slot="addonBefore" v-decorator="['prefix', { initialValue: '86' }]" style="width: 70px">
                                        <a-select-option value="86"> +86 </a-select-option>
                                        <a-select-option value="87"> +87 </a-select-option>
                                    </a-select>
                                </a-input>
                            </a-form-item>
                            <a-form-item>
                                <a-input v-decorator="['captcha',{ rules: [{ required: true, message: '请输入验证码' }] },]" placeholder="请输入验证码" style="position: relative"></a-input>
                                <a-button v-if="!showSecond" type="primary" @click="getCode" style="position: absolute;right: 10px; top: -6px;background: linear-gradient(90deg, #3881F8 0%, #5EA3FE 100%); border-radius: 8px;">获取验证码</a-button>
                                <div class="count-down" v-else>{{second}}<span>s</span></div>
                            </a-form-item>
                            <a-form-item>
                                <a-checkbox v-decorator="['remember', { valuePropName: 'checked', initialValue: false,},]">
                                    同意<router-link to="/contactUs?id=1">《用户服务协议》</router-link><router-link to="/contactUs?id=2">《隐私协议》</router-link>
                                </a-checkbox>
                            </a-form-item>
                        </a-form>
                        <a-button type="primary" class="login-form-button" @click="handleSubmit">登录</a-button>
                        <p>还没有账号？<span @click="toRegiser" style="color: #386EFF;cursor: pointer;">去注册</span></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import Header from "../../components/Header.vue";
import { setToken } from '@/utils/auth'
export default {
    name: 'login',
    components: {
        Header
    },
    data() {
        return {
            isAccount: true,
            typePwd:'password',
            second: 60,
            showSecond: false,
            mobile: ''
        }
    },
    beforeCreate() {
        this.form = this.$form.createForm(this, { name: 'normal_login' });
    },
    created() {

    },
    methods: {
        getCode() {
            this.mobile = this.form.getFieldValue('mobile');
            if (!this.mobile) {
                this.$message.error('请输入手机号');
            } else {
                const params = {
                    mobile: this.mobile,
                    event: 'mobilelogin'
                }
                this.$request.post('/api/sms/send', params).then(res => {
                    if (res.code == 1) {
                        console.log(res.data);
                        this.$message.success('验证码发送成功');
                        this.second = 60;
                        this.showSecond = true;
                        this.countDown();
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            }
        },
        // 倒计时
        countDown () {
            const setInnt = setInterval(() => {
                if (parseInt(this.second) > 1) {
                    this.second = this.second - 1;
                } else {
                    clearInterval(setInnt);
                    this.second = 60;
                    this.showSecond = false;
                }
            }, 1000);
        },
        handleSubmit(e) {
            e.preventDefault();
            this.form.validateFields((err, values) => {
                console.log('submit!', err, values);
                if (!err && values.remember) {
                    if (this.isAccount) {
                        this.$request.post('api/user/login', values).then(res => {
                            if (res.code == 1) {
                                this.$message.success('登录成功！');
                                setTimeout(() => {
                                    localStorage.setItem('userinfo', JSON.stringify(res.data.userinfo));
                                    localStorage.setItem('token', res.data.userinfo.token);
                                    setToken(res.data.userinfo.token);
                                    this.$router.push('/');
                                },300)
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                    } else {
                        this.$request.post('api/user/mobilelogin', values).then(res => {
                            if (res.code == 1) {
                                this.$message.success('登录成功！');
                                setTimeout(() => {
                                    localStorage.setItem('userinfo', JSON.stringify(res.data.userinfo));
                                    localStorage.setItem('token', res.data.userinfo.token);
                                    this.$router.push('/');
                                },300)
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                } else {
                    this.$message.error('请勾选同意协议');
                }
            });
        },
        showPwd (){
            this.typePwd = this.typePwd === 'password' ? 'text' : 'password';
        },
        toRegiser() {
            this.$router.push('/register')
        }
    }
}
</script>
<style lang="less" scoped>
.login-wrap{
    .login-box{
        width: 100%;
        height: 100vh;
        padding-top: 80px;
        overflow: auto;
        background: url('../../assets/images/login-bg.png') no-repeat center center;
        background-size: 100% 100%;
    }
    .container{
        max-width: 1200px;
        margin: 0 auto;
        height: calc(100vh - 73px);
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        padding-top: 70px;
        .logo{
            text-align: left;
            img{
                width: 334px;
                height: 62px;
            }
            p{
                font-weight: 400;
                font-size: 18px;
                color: #636363;
                margin-top: 30px;
            }
        }
        .login-form{
            width: 480px;
            // height: 578px;
            background: #FFFFFF;
            box-shadow: 0px 0px 14px 0px rgba(0,69,207,0.08);
            border-radius: 12px;
            padding: 30px 0;
            .login-tab{
                display: flex;
                justify-content: center;
                align-items: center;
                padding: 30px 40px;
                .account{
                    font-size: 18px;
                    font-weight: bold;
                    color: #333333;
                    cursor: pointer;
                    margin-right: 60px;
                    padding-bottom: 10px;
                    &.active{
                        font-weight: bold;
                        border-bottom: #3881F8 4px solid;
                        border-radius: 4px;
                    }
                }
                .shouji{
                    font-size: 18px;
                    font-weight: bold;
                    color: #999999;
                    cursor: pointer;
                    padding-bottom: 10px;
                    &.active{
                        font-weight: bold;
                        border-bottom: #3881F8 4px solid;
                        border-radius: 4px;
                    }
                }
            }
            .form-item{
                margin-bottom: 20px;
                .formclass{
                    padding: 0 40px;
                }
                .login-form-button{
                    width: 362px;
                    height: 52px;
                    background: linear-gradient( 90deg, #3881F8 0%, #5EA3FE 100%);
                    font-weight: 400;
                    font-size: 17px;
                    color: #FFFFFF;
                    line-height: 52px;
                    display: block;
                    margin: 20px auto;
                }
            }
        }
        .count-down{
            font-size: 16px;
            font-family: PingFang SC-Regular, PingFang SC, sans-serif;
            font-weight: 400;
            color: #FF6700;
            margin-left:21px;
            position: absolute;
            right: 10px;
            top: -6px;
        }
    }
}
/deep/ .ant-input{
  height: 54px;
  background: #F2F4F9;
  border: none;
}
</style>